<template>
  <a-layout>
    <a-layout-content style="height: calc(100vh - 88px); padding: 2px">
      <VxeBasicTable ref="TableRef" v-bind="options">
        <template #toolbar_buttons>
          <ToolBtn
            :filter-call="handleOpenFilter"
            :upload="handleUpload"
            :delete-call="handleDelete"
            :show-export="false"
            :show-import="false"
            :show-delete="false"
          />
        </template>
        <template #action="{ row }">
          <Action
            :record="row"
            @download="handleDownload"
            @delete="handleDelete"
          />
        </template>
      </VxeBasicTable>
    </a-layout-content>
    <ConfigEdit v-model:visible="showEdit" :data="current" @done="reload" />
    <TableFilter
      ref="FilterRef"
      @done="reload"
      v-model:visible="ifSHowFilter"
      :filter-cols="Columns"
    />
  </a-layout>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { messageLoading } from 'ele-admin-pro/es';
  import ConfigEdit from './components/config-edit.vue';
  import {
    pageFiles,
    removeFile,
    uploadFile,
    downloadFile
  } from '@/api/system/file';
  import type { FileRecord } from '@/api/system/file/model';
  import { useConfig } from './config';

  const {
    TableRef,
    FilterRef,
    Columns,
    TableFilter,
    ToolBtn,
    Action,
    options
  } = useConfig(pageFiles);

  // 当前选中数据
  const current = ref<FileRecord>();

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  const ifSHowFilter = ref(false);

  /* 刷新表格 */
  const reload = () => {
    TableRef.value?.commitProxy('query');
  };
  /* 上传文件 */
  const handleUpload = ({ file }) => {
    if (file.size / 1024 / 1024 > 100) {
      message.error('大小不能超过 100MB');
      return false;
    }
    const hide = messageLoading({
      content: '上传中..',
      duration: 0,
      mask: true
    });
    uploadFile(file)
      .then(() => {
        hide();
        message.success('上传成功');
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
    return false;
  };
  /* 下载文件 */
  const handleDownload = (_row: FileRecord) => {
    const { fileName, suffix, id } = _row;
    downloadFile(_row.id).then((res: any) => {
      const blob = new Blob([res], {
        type: 'application/octet-stream'
      });
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = (fileName ? fileName : id) + (suffix ? suffix : '.unknow');
      a.click();
      window.URL.revokeObjectURL(url);
    });
  };
  const handleOpenFilter = () => {
    ifSHowFilter.value = true;
  };

  /* 删除 */
  const handleDelete = (row: FileRecord) => {
    const hide = messageLoading('请求中..', 0);
    removeFile(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };
</script>

<script lang="ts">
  export default {
    name: 'SystemFile'
  };
</script>
